<template>
  <div class="detail-container">
    <el-form
      ref="postForm"
      :model="postForm"
      :rules="rules"
      label-width="100px"
      label-suffix=":"
    >

      <sticky :z-index="10" :class-name="'sub-navbar ' + postForm.color">
        <el-button v-loading="loading" type="success" @click="submitForm">
          {{ isEdit ? '更新' : '保存' }}
        </el-button>
      </sticky>

      <div class="app-container" style="padding: 10px 20px">
        <warn />

        <el-row>
          <el-col :span="22">
            <el-form-item label="名称" prop="name">
              <el-input v-model.trim="postForm.name" />
            </el-form-item>
            <el-form-item label="状态" style="width: 200px">
              <el-switch v-model="postForm.status" />
            </el-form-item>
            <el-form-item label="下单规则">
              <el-input v-model="postForm.func_order" />
            </el-form-item>
            <el-form-item label="支付规则">
              <el-input v-model="postForm.func_pay" />
            </el-form-item>
            <el-form-item label="返佣规则">
              <el-input v-model="postForm.func_commission" />
            </el-form-item>
            <el-form-item label="售后规则">
              <el-input v-model="postForm.func_service" />
            </el-form-item>

          </el-col>
        </el-row>
      </div>

    </el-form>
  </div>
</template>

<script>
import api from '@/api'
import Sticky from '@/components/Sticky'
import Warn from './Warn'
export default {
  name: 'AreaDetail',
  components: {
    Sticky,
    Warn
  },
  props: {
    /**
     * 是否为编辑
     */
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    const titleValidate = (rule, value, callback) => {
      if (value === undefined || value.length <= 0) {
        callback(new Error('请输入名称'))
      } else {
        callback()
      }
    }
    return {
      id: -1,
      loading: false,
      postForm: {
        color: 'draft',
        status: true
      },
      rules: {
        name: [{ validator: titleValidate, trigger: 'blur' }]
      }
    }
  },
  created() {
    /**
     * 获取编辑数据
     */
    if (this.isEdit) {
      this.id = (this.$route.params && this.$route.params.id) || -1
      this.fetchData(this.id)
    }
  },
  methods: {
    /**
     * 获取编辑数据
     */
    async fetchData(id) {
      const response = await api.area.view({ id })
      this.postForm = Object.assign({}, response.data)
      // 状态
      this.postForm.status = +this.postForm.status === 1
    },
    /**
     * 提交表单
     */
    submitForm() {
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true
          // 状态
          const status = this.postForm.status ? 1 : 0
          api.area.edit({
            id: this.id,
            ...this.postForm,
            status
          }).then(response => {
            this.loading = false
            this.$notify({
              title: '成功',
              message: response.message || '操作成功',
              type: 'success',
              duration: 2000
            })
            this.$router.push(`/product/area/edit/${response.data.id}`)
          }).finally(() => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.detail-container {
  //
}
</style>
